﻿/// ZaxControls
/// Programmed by Leland Ede
/// Website: http://www.zaxisstudios.com
/// E-Mail: codeplex@zaxis-studios.com
/// Create: 4/19/2014

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Design;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Text;
using System.Web;

namespace ZaxLib.Imaging
{
	public class CIcon
	{
		#region Parameters

		private Image _imgData = null;
		public Image ImgData
		{
			get
			{
				if (_imgData == null)
				{
					String file = Path.Combine(FilePath, FileName);
					if(File.Exists(file))
					{
						try
						{
							_imgData = Bitmap.FromFile(file);
						}
						catch (Exception ex)
						{
							// TODO: Log exception
						}
					}
				}

				return _imgData;
			}
		}

		private String _fileName = null;
		public String FileName
		{
			get
			{
				return _fileName;
			}
			set
			{
				_fileName = value;
			}
		}

		public String _filePath = null;
		public String FilePath
		{
			get
			{
				return _filePath;
			}
			set
			{
				// TODO: Make sure the file path conforms to operating system limits.
				_filePath = value;
			}
		}

		private String _webPath = null;
		public String WebPath
		{
			get
			{
				String s = _webPath;
				return (s == null ? String.Empty : s);
			}
			set
			{
				_webPath = value;
			}
		}

		/// <summary>
		/// Image Height
		/// </summary>
		public String Height
		{
			get
			{
				String s = "0";
				if (ImgData != null)
				{
					s = String.Format("{0}", ImgData.Height);
				}
				return s;
			}
		}

		/// <summary>
		/// Image Width
		/// </summary>
		public String Width
		{
			get
			{
				String s = "0";
				if (ImgData != null)
				{
					s = String.Format("{0}", ImgData.Width);
				}
				return s;
			}
		}

		#endregion

		#region Constructors

		public CIcon(String fileName, HttpRequest request)
		{
			if (fileName[0] == '~')
			{
				String sFile = request.MapPath(fileName);
				this.FilePath = Path.GetDirectoryName(sFile);
				this.FileName = Path.GetFileName(sFile);
				this.WebPath = fileName.Replace("~/", request.ApplicationPath);
			}
		}

		public CIcon(String fileName, String svrPath, String webPath)
		{
			this.FileName = Path.GetFileName(fileName);
			this.FilePath = svrPath;
			this.WebPath = webPath;
		}

		#endregion
	}
}
